<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <el-icon class="logo-icon"><Football /></el-icon>
          <h1>乒乓球培训管理系统</h1>
        </div>
        <p class="subtitle">专业的培训机构管理平台</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 演示账户 -->
      <div class="demo-accounts">
        <h4>演示账户</h4>
        <div class="demo-grid">
          <div 
            class="demo-item"
            v-for="account in demoAccounts"
            :key="account.username"
            @click="quickLogin(account)"
          >
            <div class="demo-role">{{ account.roleName }}</div>
            <div class="demo-credentials">{{ account.username }} / {{ account.password }}</div>
          </div>
        </div>
      </div>

      <!-- 临时直接进入按钮 -->
      <div class="quick-access">
        <el-divider>或</el-divider>
        <el-button 
          type="success" 
          size="large" 
          class="direct-btn"
          @click="directEnter"
        >
          直接进入系统 (跳过登录)
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { User, Lock, Football } from '@element-plus/icons-vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ]
}

// 登录加载状态
const loading = ref(false)

// 演示账户
const demoAccounts = [
  { username: 'admin', password: 'admin123', role: 'super_admin', roleName: '超级管理员' },
  { username: 'manager1', password: 'manager123', role: 'campus_admin', roleName: '校区管理员' },
  { username: 'student1', password: 'student123', role: 'student', roleName: '学员' },
  { username: 'coach1', password: 'coach123', role: 'coach', roleName: '教练' }
]

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    // 模拟登录验证
    const account = demoAccounts.find(
      acc => acc.username === loginForm.username && acc.password === loginForm.password
    )

    if (account) {
      // 设置用户信息
      await userStore.login({
        id: '1',
        username: account.username,
        name: getRoleName(account.role),
        role: account.role,
        avatar: '',
        campus: account.role === 'super_admin' ? '总部' : '中心校区',
        phone: '138-0000-0001',
        email: `${account.username}@example.com`
      })

      ElMessage.success('登录成功！')
      router.push('/dashboard')
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}

// 快速登录
const quickLogin = (account: any) => {
  loginForm.username = account.username
  loginForm.password = account.password
  handleLogin()
}

// 直接进入系统（跳过登录）
const directEnter = () => {
  // 默认以学员身份进入
  userStore.login({
    id: '1',
    username: 'demo_user',
    name: '演示用户',
    role: 'student',
    avatar: '',
    campus: '中心校区',
    phone: '138-0000-0001',
    email: 'demo@example.com'
  })

  ElMessage.success('以演示用户身份进入系统')
  router.push('/dashboard')
}

// 获取角色名称
const getRoleName = (role: string) => {
  const roleMap: Record<string, string> = {
    'super_admin': '超级管理员',
    'campus_admin': '校区管理员张主任',
    'student': '学员张小明',
    'coach': '教练李老师'
  }
  return roleMap[role] || '用户'
}
</script>

<style scoped lang="scss">
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #ff7700, #ffa64d);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 450px;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
  
  .logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 8px;
    
    .logo-icon {
      font-size: 32px;
      color: #ff7700;
    }
    
    h1 {
      font-size: 24px;
      color: #1f2937;
      margin: 0;
    }
  }
  
  .subtitle {
    color: #6b7280;
    font-size: 14px;
    margin: 0;
  }
}

.login-form {
  margin-bottom: 24px;
  
  .login-btn {
    width: 100%;
    height: 44px;
    font-size: 16px;
    font-weight: 600;
    background: linear-gradient(135deg, #ff7700, #ffa64d);
    border: none;
    
    &:hover {
      background: linear-gradient(135deg, #e66600, #ff9933);
    }
  }
}

.demo-accounts {
  margin-bottom: 24px;
  
  h4 {
    text-align: center;
    color: #374151;
    font-size: 14px;
    margin: 0 0 16px 0;
  }
  
  .demo-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  
  .demo-item {
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    padding: 12px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    
    &:hover {
      background: #fff5e6;
      border-color: #ff7700;
      transform: translateY(-1px);
    }
    
    .demo-role {
      font-size: 12px;
      font-weight: 600;
      color: #ff7700;
      margin-bottom: 4px;
    }
    
    .demo-credentials {
      font-size: 11px;
      color: #6b7280;
    }
  }
}

.quick-access {
  text-align: center;
  
  .direct-btn {
    width: 100%;
    height: 40px;
    font-weight: 500;
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-container {
    padding: 12px;
  }
  
  .login-card {
    padding: 24px;
  }
  
  .demo-grid {
    grid-template-columns: 1fr;
  }
}
</style>
